package com.cyz.web.app.thread.threadlocalDemo;

import lombok.extern.slf4j.Slf4j;


@Slf4j
public class ThreadLocalDemo {


    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    private static InheritableThreadLocal<String> inHerThreadLocal = new InheritableThreadLocal<>();


    public static void main(String[] args) {
        ThreadLocalDemo client = new ThreadLocalDemo();
        //threadLocal是线程安全的, 父子线程无法传递数据
//        client.demo1();
        //线程安全的
        client.demo2();
    }

    void demo2(){
        inHerThreadLocal.set("liSi");

       Thread thread =  new Thread(()->{
            log.info("子线程获取的inHerThreadLocal的值 {}", inHerThreadLocal.get());
        });

       thread.start();



    }


    //子线程无法获取父线程的本地变量
    void demo1() {
        threadLocal.set("zhangSan");
        new Thread(() -> {
            log.info("子线程获取的threadLocal的值 {}", threadLocal.get());
        }).start();
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
